package tr.edu.metu.is.mega.client.course.instructor.students;

import java.util.ArrayList;
import java.util.List;

import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.widgets.MegaComboBox;
import tr.edu.metu.is.mega.common.dto.ActivityDTO;
import tr.edu.metu.is.mega.common.dto.BaseDTO;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.ReviewerDTO;
import tr.edu.metu.is.mega.common.dto.TeamDTO;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Edit dialog for reviewers.
 */
public class ReviewerEditDialog extends EditDialog<ReviewerDTO> {

    private CourseDTO course;

    private MegaComboBox<BeanModel> comboBoxActivity;

    private BeanModelFactory modelFactoryActivity = null;

    private TeamField teamField;

    private Integer activityToReview;

    /**
     * Default constructor.
     */
    public ReviewerEditDialog(CourseDTO course, Integer activityToReview) {
        super(TeamDTO.class.getName(), course, activityToReview);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void beforeInitForm(BaseDTO baseDTO) {
        course = (CourseDTO) baseDTO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {
        setSize(340, 150);
        setHeading("Assign Reviewer");
        activityToReview = (Integer) params[0];
        modelFactoryActivity = BeanModelLookup.get().getFactory(ActivityDTO.class);

        comboBoxActivity = new MegaComboBox<BeanModel>(null);
        ListStore<BeanModel> store = new ListStore<BeanModel>();
        List<ActivityDTO> list = new ArrayList<ActivityDTO>();
        for (ActivityDTO dto : course.getActivities()) {
            if (dto.getPkid() != activityToReview) {
                list.add(dto);
            }
        }
        store.add(modelFactoryActivity.createModel(list));
        comboBoxActivity.setStore(store);
        comboBoxActivity.setDisplayField(ActivityDTO.FIELD_DESCRIPTION);

        comboBoxActivity.setFieldLabel("Review By");
        parent.add(comboBoxActivity, new FormData("96%"));

        teamField = new TeamField("", course);
        teamField.setFieldLabel("Reviewer Team");
        parent.add(teamField, new FormData("96%"));

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected ReviewerDTO guiToDTO() {
        ReviewerDTO dto = new ReviewerDTO();
        dto.setReviewerTeam((TeamDTO) teamField.getValue());
        dto.setActivity(activityToReview);
        dto.setReviewActivity(((ActivityDTO) comboBoxActivity.getSelection().get(0).getBean()).getPkid());
        return dto;
    }

}
